package com.flbill.service.impl;

import com.flbill.common.result.PageResult;
import com.flbill.common.utils.BaseContext;
import com.flbill.mapper.TransactionsMapper;
import com.flbill.pojo.dto.ExpenseDTO;
import com.flbill.pojo.dto.TransactionsDTO;
import com.flbill.pojo.dto.TransactionsPageDTO;
import com.flbill.pojo.entity.Expense;
import com.flbill.pojo.vo.TransactionsPageVO;
import com.flbill.service.TransactionsService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class TransactionsServiceImpl implements TransactionsService {
    private final TransactionsMapper transactionsMapper;
    public TransactionsServiceImpl(TransactionsMapper transactionsMapper) {
        this.transactionsMapper = transactionsMapper;
    }

    @Override
    public void addExpense(ExpenseDTO expenseDTO) {
        Expense expense = new Expense();
        BeanUtils.copyProperties(expenseDTO, expense);
        expense.setCreateTime(LocalDateTime.now());
        expense.setUpdateTime(LocalDateTime.now());
        expense.setExpenseTime(LocalDateTime.now());
        expense.setExpenseClass(expenseDTO.getType());
        expense.setUserId(BaseContext.getCurrentId());
        transactionsMapper.insert(expense);
    }

    @Override
    public void addIncome(com.flbill.pojo.dto.IncomeDTO incomeDTO) {
        com.flbill.pojo.entity.Income income = new com.flbill.pojo.entity.Income();
        org.springframework.beans.BeanUtils.copyProperties(incomeDTO, income);
        income.setCreateTime(java.time.LocalDateTime.now());
        income.setUpdateTime(java.time.LocalDateTime.now());
        income.setIncomeTime(java.time.LocalDateTime.now());
        income.setIncomeClass(incomeDTO.getType());
        income.setUserId(com.flbill.common.utils.BaseContext.getCurrentId());
        transactionsMapper.insertIncome(income);
    }

    @Override
    public PageResult ExpensePagelist(TransactionsPageDTO transactionsPageDTO) {
        PageHelper.startPage(transactionsPageDTO.getPage(), transactionsPageDTO.getPageSize());
        Integer userId = BaseContext.getCurrentId();
        LocalDate start = transactionsPageDTO.getStartDate();
        //获取结束时间,往后推两天
        LocalDate end = transactionsPageDTO.getEndDate().plusDays(2);
        Page<TransactionsPageVO> page = transactionsMapper.ExpensePagelist(userId, start, end);
        //遍历集合
        page.getResult().forEach(item -> {
            item.setType("Expense");
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public PageResult IncomePagelist(TransactionsPageDTO transactionsPageDTO) {
        PageHelper.startPage(transactionsPageDTO.getPage(), transactionsPageDTO.getPageSize());
        Integer userId = BaseContext.getCurrentId();
        LocalDate start = transactionsPageDTO.getStartDate();
        //获取结束时间,往后推两天
        LocalDate end = transactionsPageDTO.getEndDate().plusDays(2);
        Page<TransactionsPageVO> page = transactionsMapper.IncomePageLIst(userId, start, end);
        //遍历输出
        page.getResult().forEach(item -> {
            item.setType("Income");
        });
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void delete(Integer id,String type) {
        if (type.equals("Expense")) {
            transactionsMapper.deleteExpense(id);
            return ;
        }
        if (type.equals("Income")) {
            transactionsMapper.deleteIncome(id);
        }
    }

    @Override
    public void update(Integer id, TransactionsDTO transactionsDTO) {
        String type = transactionsDTO.getType();
        transactionsDTO.setId(id);
        if ("Expense".equals(type)) {
            transactionsMapper.updateExpense(id, transactionsDTO.getExpense());
        }
        if ("Income".equals(type)) {
            transactionsMapper.updateIncome(id, transactionsDTO.getIncome());
        }
    }


}
